package com.test.dao;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
import org.apache.hadoop.io.IOUtils;

/**
 * ʹ��FileSystem
 * 
 * @author Administrator
 */
public class UploadDataToHdfs {
	public static final String HDFS_PATH = "hdfs://hadoop:9000";
	public static final String DIR_PATH = "/qinzhen";
	// public static final String FILE_PATH
	// ="/user/hive/warehouse/qin.db/log1/log";studentmanagesystem.db
	public static final String FILE_PATH = "/user/hive/warehouse/ni.db/hd_student/student";

	public static void main(String[] args) throws Exception {
		final FileSystem fileSystem = FileSystem.get(new URI(HDFS_PATH),
				new Configuration());
		// /user/hive/warehouse/qin.db/log1
		// �����ļ���
		// makeDirectory(fileSystem);
		// 上传单个文件
		uploadData(fileSystem);
		// 上传多个文件
		// uploadData1(fileSystem,20);

		// downLoadData(fileSystem);

		// deleteFile(fileSystem);

		// getHDFSNodes(fileSystem);

		// copyFolder("E:/baiduplayer",DIR_PATH,fileSystem);

		// getFileLocal(fileSystem);
	}

	private static void deleteFile(final FileSystem fileSystem)
			throws IOException {
		fileSystem.delete(new Path(FILE_PATH), true);
	}

	private static void downLoadData(final FileSystem fileSystem)
			throws IOException {
		long start = System.currentTimeMillis();
		File file = new File("E:/qin11.avi");
		FileOutputStream out = new FileOutputStream(file);
		final FSDataInputStream in = fileSystem.open(new Path(FILE_PATH));
		// IOUtils.copyBytes(in, System.out, 1024,true);
		IOUtils.copyBytes(in, out, 1024, true);
		out.flush();
		long end = System.currentTimeMillis();
		System.out.println("time = " + (end - start) / 1000);
	}

	private static void makeDirectory(final FileSystem fileSystem)
			throws IOException {
		// �����ļ���
		fileSystem.mkdirs(new Path(DIR_PATH));
	}

	private static void uploadData1(final FileSystem fileSystem, int n)
			throws IOException, FileNotFoundException {
		// �ϴ��ļ�
		long start = System.currentTimeMillis();
		for (int i = 1; i < n + 1; i++) {
			String outpath = "/test1/hive/qin.db/log/log" + i;
			String inpath;
			int j = i + 8;
			if (j < 10)
				inpath = "C:/Users/Administrator/Desktop/maven_hadoop_template-kpi_v1/maven_hadoop_template-kpi_v1/logfile/2015010"
						+ j + "http_log";
			else
				inpath = "C:/Users/Administrator/Desktop/maven_hadoop_template-kpi_v1/maven_hadoop_template-kpi_v1/logfile/201501"
						+ j + "http_log";
			final FSDataOutputStream out = fileSystem.create(new Path(outpath));

			final FileInputStream in = new FileInputStream(inpath);
			IOUtils.copyBytes(in, out, 1024, true);
		}
		long end = System.currentTimeMillis();
		System.out.println("time = " + (end - start) / (1000));
	}

	private static void uploadData(final FileSystem fileSystem)
			throws IOException, FileNotFoundException {
		// �ϴ��ļ�
		final FSDataOutputStream out = fileSystem.create(new Path(FILE_PATH));
		long start = System.currentTimeMillis();
		// final FileInputStream in = new
		// FileInputStream("C:/Users/Administrator/Desktop/maven_hadoop_template-kpi_v1/maven_hadoop_template-kpi_v1/logfile/http_log");
		final FileInputStream in = new FileInputStream(
				"C:/Users/Administrator/Desktop/maven_hadoop_template-kpi_v1/maven_hadoop_template-kpi_v1/logfile/student.txt");
		IOUtils.copyBytes(in, out, 1024, true);
		long end = System.currentTimeMillis();
		System.out.println("time = " + (end - start) / (1000));
	}

	public static void getFileLocal(final FileSystem fileSystem)
			throws Exception {
		Path path = new Path(FILE_PATH);
		// ��ȡ�ļ�ϵͳ������ļ���Ϣ
		FileStatus fileStatus = fileSystem.getFileStatus(path);
		// ��ȡ�ļ������Ϣ
		BlockLocation[] blockLocations = fileSystem.getFileBlockLocations(
				fileStatus, 0, fileStatus.getLen());
		for (int i = 0; i < blockLocations.length; i++) {
			String[] hosts = blockLocations[i].getHosts();
			for (String host : hosts)
				System.out.println("block_" + i + "_location:" + host);
		}
	}

	// ��ȡHDFS�ж��ٸ����ŵ�Datanode
	public static void getHDFSNodes(final FileSystem fileSystem)
			throws IOException {
		DistributedFileSystem hdfs = (DistributedFileSystem) fileSystem;
		DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats();
		for (int i = 0; i < dataNodeStats.length; i++) {
			System.out.println("DataNode_" + i + "_Name"
					+ dataNodeStats[i].getHostName());
		}
	}

	public static void IOCopy(String path, String path1, FileSystem fileSystem)
			throws Exception {
		File file = new File(path);
		// File file1 = new File(path1);
		final boolean isExists = fileSystem.exists(new Path(path1));

		if (!file.exists()) {
			System.out.println(file.getName() + "�ļ�������");
		} else {
			System.out.println("����");
		}
		// byte[] b = new byte[(int) file.length()];
		if (file.isFile()) {
			try {
				// FileInputStream is = new FileInputStream(file);
				// FileOutputStream ps = new FileOutputStream(file1);
				final FSDataOutputStream out = fileSystem
						.create(new Path(path1));
				long start = System.currentTimeMillis();
				final FileInputStream in = new FileInputStream(path);
				IOUtils.copyBytes(in, out, 1024, true);
				// is.read(b);
				// ps.write(b);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else if (file.isDirectory()) {
			if (!isExists)// isExists.exists())
			{
				fileSystem.mkdirs(new Path(path1));
				// file1.mkdir();
			}
			String[] list = file.list();
			for (int i = 0; i < list.length; i++) {
				// IOCopy(path + "/" + list[i], path1 + "/" + list[i]);
				IOCopy(path + "/" + list[i], path1 + "/" + list[i], fileSystem);
			}
		}
	}

	public static void copyFolder(String path, String path1,
			FileSystem fileSystem) throws Exception {

		File file = new File(path);
		path1 = path1 + "/" + file.getName();
		// File file1 = new File(path1);
		final boolean isExists = fileSystem.exists(new Path(path1));
		if (file.isDirectory() && (!isExists)) {
			// file1.mkdir();
			fileSystem.mkdirs(new Path(path1));
		}
		IOCopy(path, path1, fileSystem);
	}

}
